package org.example.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import io.swagger.annotations.ApiOperation;
import org.example.dto.UserAdminDTO;
import org.example.dto.UserAreaDTO;
import org.example.dto.UserLogoutStatusDTO;
import org.example.param.UserAuthParam;
import org.example.result.Result;
import org.example.service.TUserAuthService;
import org.example.vo.ConditionVO;
import org.example.vo.PageResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TUserAuth)表控制层
 *
 * @author makejava
 * @since 2023-06-14 14:09:34
 */
@RestController
public class TUserAuthController {
    /**
     * 服务对象
     */
    @Resource
    private TUserAuthService tUserAuthService;

    @Value("${sa-token.key}")
    private String key;

    @ApiOperation(value = "获取用户区域分布")
    @GetMapping("/admin/users/area")
    public Result<List<UserAreaDTO>> listUserAreas(ConditionVO conditionVO) {
        return Result.ok(tUserAuthService.listUserAreas(conditionVO));
    }

    @ApiOperation(value = "查询后台用户列表")
    @GetMapping("/admin/users")
    public Result<PageResult<UserAdminDTO>> listUsers(ConditionVO conditionVO) {
        return Result.ok(tUserAuthService.listUsers(conditionVO));
    }
    @ApiOperation("用户登出")
    @PostMapping("/users/logout")
    public Result<UserLogoutStatusDTO> logout() {
        return Result.ok(tUserAuthService.logout());
    }

    /**
     * 注册
     * @param userAuthParam 参数
     * */
    @PostMapping("/users/register")
    public Result<?> register(@RequestBody @Validated(UserAuthParam.RegisterGroup.class) UserAuthParam userAuthParam){
        return Result.ok(this.tUserAuthService.register(userAuthParam));
    }
    /**
     * 登录
     * @param userAuthParam 登录参数
     * */
    @PostMapping("/users/login")
    public Result<?>login(@RequestBody @Validated(UserAuthParam.LoginGroup.class) UserAuthParam userAuthParam){
        return Result.ok(this.tUserAuthService.login(userAuthParam));
    }
    /**
     * 密码解密
     * @param password 密码
     * */
    @GetMapping("/decode")
    public String decode(String password){
        return SaSecureUtil.aesDecrypt(key,password);
    }


}

